//
//  AdvertiserViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/28.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AdvertiserViewController.h"
#import "HttpOrgAction.h"
#import "AddOrgViewController.h"
#import "AdvertiserDetailViewController.h"

@interface AdvertiserViewController ()
//@property (nonatomic,strong) NSArray *listArray;
@end

@implementation AdvertiserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"广告商";
    
    if ([Utils getUserPermission:@"addorg"]) {
        UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
        [rightbutton setTitle:@"+" forState:UIControlStateNormal];
        [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(addAdvertiserAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    }
    
    
    self.tableView.separatorStyle = 0;
    [self loadData];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)loadData{
    if (self.tableView.mj_header) {
        [self.tableView.mj_header beginRefreshing];
        
    }else{
        __weak AdvertiserViewController *filterView = self;
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [filterView loadHeader];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        self.tableView.mj_header = header;
        
        [self.tableView.mj_header beginRefreshing];
        
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFooter)];
        
        OrgActionInfo *orgActionInfo = [[OrgActionInfo alloc] init];
        orgActionInfo.orgType = @"3";
        [self setClass:@"HttpOrgAction" action:@selector(postSearchOrgList:block:) object:orgActionInfo];
    }
    
}

-(void)addAdvertiserAction:(id)sender{
    AddOrgViewController *addOrgViewController = [[AddOrgViewController alloc] initWithOrgType:OrgTypeAdvertiser withBlock:^{
        [self loadData];

    }];
    [self.navigationController pushViewController:addOrgViewController animated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = 0;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = DefaultCellLineColor;
        [cell.contentView addSubview:lineView];
        lineView.sd_layout.leftSpaceToView(cell.contentView, 10).bottomSpaceToView(cell.contentView, 0).rightSpaceToView(cell.contentView, 0).heightIs(1);
    }
    // Configure the cell...
    OrgInfo *orgInfo = self.resultArray[indexPath.row];
    cell.textLabel.text = orgInfo.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OrgInfo *orgInfo = self.resultArray[indexPath.row];
    AdvertiserDetailViewController *advertiserDetailViewController = [[AdvertiserDetailViewController alloc] initWithOrgInfo:orgInfo];
    [self.navigationController pushViewController:advertiserDetailViewController animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
